#include "thread.hpp"
#include "task.hpp"
#include "threadpool.hpp"
#include <unistd.h>


using namespace MYthread;
void* thread_task(void* arg)
{
    Thread* t = static_cast<Thread*>(arg);

    std::string r;
    char sign[5] = "+-*/";
    int x = rand() % 20 + 1;
    int y = rand() % 20 + 1;
    int op = rand() % 4;
    Task task(x, y, sign[op]);
    task.record(r);
    std::cout << t->thread_name() << ": 收到任务" << r << std::endl;
    std::cout << "结果为：" << task.result() << std::endl;
    sleep(1);

    return nullptr;
}

void* push_task(void* arg)
{
    Threadpool<Task>* tp = static_cast<Threadpool<Task>*>(arg);
    char sign[5] = "+-*/";
    while(true)
    {
        int x = rand() % 20 + 1;
        int y = rand() % 20 + 1;
        int op = rand() % 4;
        Task task(x, y, sign[op]);
        tp->push(task);
        sleep(1);
    }

    return nullptr;
}

int main()
{
    Thread t2;
    Threadpool<Task>* tp = new Threadpool<Task>;

    t2.thread_start(push_task, (void*)tp);
    tp->run();

    while(true)
    {
        sleep(1);
    }

    return 0;
}